// Mocks a delay to simulate network latency
const mockFetch = (data, delay = 500) => 
    new Promise(resolve => setTimeout(() => resolve({ success: true, data }), delay));

// Mock data generators
const generateHeatmapData = () => {
    const data = [];
    for (let i = 0; i < 52; i++) { // 52 weeks
        const week = [];
        for (let j = 0; j < 7; j++) { // 7 days
            week.push({
                date: `2024-W${i + 1}-D${j + 1}`,
                value: Math.floor(Math.random() * 500)
            });
        }
        data.push(week);
    }
    return data;
};

const generateTrendData = (type) => {
    const labels = ['一月', '二月', '三月', '四月', '五月', '六月'];
    const data = [];
    const forecast = [];
    let lastValue = Math.random() * 5000;
    for (let i = 0; i < labels.length; i++) {
        lastValue += (Math.random() - 0.4) * 1000;
        data.push(Math.max(0, Math.round(lastValue)));
    }
    let lastForecastValue = data[data.length - 1];
     for (let i = 0; i < 3; i++) { // 3 months forecast
        lastForecastValue += (Math.random() - 0.4) * 1000;
        forecast.push(Math.max(0, Math.round(lastForecastValue)));
    }
    return { labels, data, forecast };
};

// API mock object
export default {
    getRealtimeStats() {
        return mockFetch([
            { key: 'online_users', icon: '👥', label: '在线用户', value: Math.floor(Math.random() * 2000), trend: (Math.random() * 20 - 10).toFixed(1), type: 'primary' },
            { key: 'active_sessions', icon: '⚡', label: '活跃会话', value: Math.floor(Math.random() * 1500), trend: (Math.random() * 20 - 10).toFixed(1), type: 'success' },
            { key: 'new_signups', icon: '🎉', label: '今日新注冊', value: Math.floor(Math.random() * 500), trend: (Math.random() * 20 - 10).toFixed(1), type: 'warning' },
            { key: 'revenue', icon: '💰', label: '今日收入 (元)', value: (Math.random() * 20000).toFixed(2), trend: (Math.random() * 20 - 10).toFixed(1), type: 'danger' },
        ]);
    },
    getHeatmapData() {
        return mockFetch(generateHeatmapData());
    },
    getAiInsights() {
        return mockFetch([
            { id: 1, type: 'positive', icon: '👍', title: '核心用户群参与度上升', description: '过去7天，"开发人员"角色的用户在课程视频上的平均观看时长增加了15%。', confidence: 95 },
            { id: 2, type: 'negative', icon: '👎', title: '移动端新用户流失风险', description: '通过移动端注册的新用户在24小时内的卸载率相比上周同期增加了8%。建议优化移动端引导流程。', confidence: 88 },
            { id: 3, type: 'neutral', icon: '💡', title: '新课程"Vue.js进阶"受关注', description: '新发布的"Vue.js高级应用"课程页面浏览量高于平均水平，但转化率较低。', confidence: 75 }
        ]);
    },
    getTrendData() {
        return mockFetch({
            users: generateTrendData('users'),
            activities: generateTrendData('activities'),
            revenue: generateTrendData('revenue'),
        });
    },
    getBehaviorMetrics() {
        return mockFetch([
            { key: 'avg_session_duration', label: '平均会话时长', value: '15m 45s', percentage: 70, color: '#409EFF' },
            { key: 'bounce_rate', label: '跳出率', value: '23.4%', percentage: 23.4, color: '#F56C6C' },
            { key: 'conversion_rate', label: '课程购买转化率', value: '5.8%', percentage: 5.8, color: '#67C23A' }
        ]);
    },
    getAnomalies() {
        return mockFetch([
            { id: 'an-1', title: '登录API延迟过高', severity: 'high', time: '2024-06-26 10:15:23' },
            { id: 'an-2', title: '来自特定IP段的访问量激增', severity: 'medium', time: '2024-06-26 09:30:10' },
            { id: 'an-3', title: '课程"数据结构"视频加载失败率上升', severity: 'high', time: '2024-06-26 08:45:00' },
        ]);
    }
}; 